package com.tianhui.consignortwofei.mvp.ui.activity.audit.driver;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.interf.ISelectItemValue;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.DictionaryType;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.DriverLicenseInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.DriverLicenseStrategy;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class DriverLicenseAuditActivity extends BaseDriverAuditActivity {

    @BindView(R.id.activity_driver_license_audit_nameInputItemView)
    InputItemView mNameInputItemView;
    @BindView(R.id.activity_driver_license_audit_numberInputItemView)
    InputItemView mNumberInputItemView;
    @BindView(R.id.activity_driver_license_audit_officeInputItemView)
    InputItemView mOfficeInputItemView;

    @BindView(R.id.activity_driver_license_audit_vehicleTypeClickItemView)
    ClickItemView mVehicleTypeClickItemView;
    @BindView(R.id.activity_driver_license_audit_startDateClickItemView)
    ClickItemView mStartDateClickItemView;
    @BindView(R.id.activity_driver_license_audit_endDateClickItemView)
    ClickItemView mEndDateClickItemView;


    private TimePickerView mStartDatePickerView;
    private TimePickerView mEndDatePickerView;
    private SelectDialog mSelectDialog;
    private String mDriverLicenseImageId;
    private String mVehicleType;

    @Override
    public int getLayoutId() {
        return R.layout.activity_driver_license_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void showDriverInfo(DriverInfo info) {
        mNameInputItemView.setContent(info.drivername);
        mNumberInputItemView.setContent(info.driverlicensenumber);
        mOfficeInputItemView.setContent(info.issuingorganizations);
        mVehicleTypeClickItemView.setContent(info.transporttypename);
        mStartDateClickItemView.setContent(info.validperiodfrom);
        mEndDateClickItemView.setContent(info.driverlicenseendtime);

        loadPicture(info.imgdriverlicense);
        mDriverLicenseImageId = info.driverlicense;
        mVehicleType = info.transporttype;
    }

    @Override
    public DriverInfo getSaveInfo() {
        String number = mNumberInputItemView.getContent();
        String office = mOfficeInputItemView.getContent();
        String vehicleType = mVehicleTypeClickItemView.getContent();
        String startDate = mStartDateClickItemView.getContent();
        String endDate = mEndDateClickItemView.getContent();
        if (TextUtils.isEmpty(number)) {
            showToast("请输入驾驶证号");
            return null;
        }

        if (TextUtils.isEmpty(office)) {
            showToast("请输入驾驶证发证机关");
            return null;
        }

        if (TextUtils.isEmpty(vehicleType)) {
            showToast("请选择准驾车型");
            return null;
        }
        if (TextUtils.isEmpty(startDate)) {
            showToast("请选择驾驶证有效期自");
            return null;
        }
        if (TextUtils.isEmpty(endDate)) {
            showToast("请选择驾驶证有效期至");
            return null;
        }
        mDriverInfo.driverlicensenumber = number;
        mDriverInfo.issuingorganizations = office;
        mDriverInfo.validperiodfrom = startDate;
        mDriverInfo.driverlicenseendtime = endDate;
        mDriverInfo.driverlicense = mDriverLicenseImageId;
        mDriverInfo.transporttype = mVehicleType;
        return mDriverInfo;
    }

    @Override
    public void nextAudit() {
        DriverInfo info = getSaveInfo();
        if (info != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_DRIVER_INFO, info);
            bundle.putInt(KeyValue.KEY_OPERATE, getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(CertificationAuditActivity.class, bundle);
        }

    }

    //有效期自
    @OnClick(R.id.activity_driver_license_audit_startDateClickItemView)
    public void selectStartDate() {
        hideKeyboard();
        if (mStartDatePickerView == null) {
            mStartDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mStartDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mStartDatePickerView.show();
    }

    //有效期至
    @OnClick(R.id.activity_driver_license_audit_endDateClickItemView)
    public void selectEndDate() {
        hideKeyboard();
        if (mEndDatePickerView == null) {
            mEndDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mEndDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mEndDatePickerView.show();
    }

    @OnClick(R.id.activity_driver_license_audit_tenYearDateButton)
    public void setTenYear() {
        String startDate = mStartDateClickItemView.getContent();
        String startDateFormat = DateUtil.validDateFormat(startDate, "yyyy-MM-dd");
        if (!TextUtils.isEmpty(startDateFormat)) {
            Date date = DateUtil.stringToDate(startDateFormat, "yyyy-MM-dd");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.roll(Calendar.YEAR, +10);
            String resultDate = df.format(calendar.getTime());
            mEndDateClickItemView.setContent(resultDate);
        } else {
            showToast("请先选择有效期自");
        }
    }

    @OnClick(R.id.activity_driver_license_audit_longDateButton)
    public void setLongDate() {
        mEndDateClickItemView.setContent("2099-12-31");
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {

        mDriverLicenseImageId = pictureInfo.id;
    }

    //准驾车型
    @OnClick(R.id.activity_driver_license_audit_vehicleTypeClickItemView)
    public void selectDriverLicenseType() {
        hideKeyboard();
        if (null == mSelectDialog) {
            requestDriverLicenseType();
        } else {
            mSelectDialog.show();
        }

    }

    //请求准驾车型
    private void requestDriverLicenseType() {
        Map<String, String> map = new HashMap<>();
        map.put("pid", "75");
        requestTypeList(DictionaryType.TYPE_DRIVER_LICENSE, map, true);
    }

    public void requestTypeList(int type, Map<String, String> map, boolean isDialog) {
        mDriverModel.dictionaryModel(this, map, isDialog, this.bindLifecycle(), new ObserverResponseListener() {
            @Override
            public void onNext(Object o) {
                CommonResponse commonResponse = (CommonResponse) o;
                if (commonResponse.getCode() == 20000) {
                    List<DictionaryInfo> list = ((CommonResponse<List<DictionaryInfo>>) o).getData();
                    if (null != list && list.size() > 0) {
                        showTypeList(type, list);
                    } else {
                        showToast("获取数据为空");
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    public void showTypeList(int type, List<DictionaryInfo> list) {
        switch (type) {
            case DictionaryType.TYPE_DRIVER_LICENSE:
                showSelectDialog(list);
                break;
        }

    }

    //准驾车型选择框
    private void showSelectDialog(List<DictionaryInfo> list) {
        hideKeyboard();
        mSelectDialog = new SelectDialog(this);
        mSelectDialog.setTitle("请选择准驾车型");
        mSelectDialog.setList(list);
        mSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<ISelectItemValue>() {
            @Override
            public void onItemClick(ISelectItemValue iSelectViewItem, int position) {
                mVehicleTypeClickItemView.setContent(iSelectViewItem.getSelectItemText());
                mVehicleType = iSelectViewItem.getSelectItemTextValue();

            }
        });
        mSelectDialog.show();
    }

    @OnClick(R.id.activity_driver_license_audit_inputLicenseNumberImageView)
    public void inputLicenseNumber() {
        mNumberInputItemView.setContent(mDriverInfo.driveridcard);
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        DriverLicenseStrategy driverLicenseStrategy = (DriverLicenseStrategy) pictureStrategyFactory.createStrategy(DriverLicenseStrategy.class);
        pictureContext = new PictureContext(driverLicenseStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<DriverLicenseInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<DriverLicenseInfo> resultInfo) {
                mDriverLicenseImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processDriverLicenseResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processDriverLicenseResult(DriverLicenseInfo resultInfo) {
        if (resultInfo == null) return;
        mNameInputItemView.setContent(resultInfo.name);
        mNumberInputItemView.setContent(resultInfo.qualificationnumber);
        mStartDateClickItemView.setContent(resultInfo.validperiodfrom);
        mEndDateClickItemView.setContent(resultInfo.driverlicenseendtime);
    }
}
